<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/core/test_utils.html">
<link rel="import" href="/tracing/extras/importer/trace_event_importer.html">
<link rel="import" href="/tracing/model/model.html">
<link rel="import" href="/tracing/ui/extras/chrome/cc/cc.html">
<link rel="import" href="/tracing/ui/extras/chrome/cc/layer_tree_quad_stack_view.html">

<script src="/tracing/extras/chrome/cc/layer_tree_host_impl_test_data.js">
</script>

<script>
'use strict';

// See https://crbug.com/1143376.
tr.b.unittest.skippedTestSuite(function() {
  test('tileCoverageRectCount', function() {
    const m = tr.c.TestUtils.newModelWithEvents([g_catLTHIEvents]);
    const p = m.processes[1];

    const instance = p.objects.getAllInstancesNamed('cc::LayerTreeHostImpl')[0];
    const lthi = instance.snapshots[0];

    const view = new tr.ui.e.chrome.cc.LayerTreeQuadStackView();
    view.layerTreeImpl = lthi.activeTree;
    view.howToShowTiles = 'none';
    view.showInvalidations = false;
    view.showContents = false;

    // There should be some quads drawn with all "show" checkboxes off,
    // but that number can change with new features added.
    const aQuads = view.generateLayerQuads();
    view.howToShowTiles = 'coverage';
    const bQuads = view.generateLayerQuads();
    const numCoverageRects = bQuads.length - aQuads.length;

    // We know we have 5 coverage rects in lthi cats.
    assert.strictEqual(numCoverageRects, 5);
  });

  test('inputEvent', function() {
    const m = tr.c.TestUtils.newModelWithEvents([g_catLTHIEvents]);
    const p = m.processes[1];

    const instance = p.objects.getAllInstancesNamed('cc::LayerTreeHostImpl')[0];
    const lthi = instance.snapshots[0];
    lthi.activeTree.tracedInputLatencies =
        [{args: {data: {coordinates: [{x: 10, y: 20}, {x: 30, y: 40}]}}}];

    const view = new tr.ui.e.chrome.cc.LayerTreeQuadStackView();
    view.layerTreeImpl = lthi.activeTree;
    view.showInputEvents = false;

    const aQuads = view.generateLayerQuads();
    view.showInputEvents = true;
    const bQuads = view.generateLayerQuads();
    const numInputEventRects = bQuads.length - aQuads.length;

    assert.strictEqual(numInputEventRects, 2);

    // We should not start loading the image until the view is added into the
    // DOM tree.
    const image = Polymer.dom(view).querySelector('#input-event');
    assert.strictEqual(getComputedStyle(image).backgroundImage, '');
    assert.strictEqual(image.src, '');

    document.body.appendChild(view);
    view.updateContents_();
    assert.notEqual(getComputedStyle(image).backgroundImage, '');
    assert.notEqual(image.src, '');
    view.remove();
  });

  test('invalidation', function() {
    const m = tr.c.TestUtils.newModelWithEvents([g_catLTHIEvents]);
    const p = m.processes[1];

    const instance = p.objects.getAllInstancesNamed('cc::LayerTreeHostImpl')[0];
    const lthi = instance.snapshots[0];

    const view = new tr.ui.e.chrome.cc.LayerTreeQuadStackView();
    view.layerTreeImpl = lthi.activeTree;
    view.showInvalidations = false;

    const aQuads = view.generateLayerQuads();
    view.showInvalidations = true;
    const bQuads = view.generateLayerQuads();
    const numInvalidationRects = bQuads.length - aQuads.length;

    // We know we have 3 invalidation rects.
    assert.strictEqual(numInvalidationRects, 3);

    const expectedRectTypes = [
      'Invalidation rect (appeared) for client1',
      'Invalidation rect (disappeared) for client2',
      'Invalidation rect'  // The non-annotated rect.
    ];
    const found = [];
    for (const quad of bQuads) {
      const i = expectedRectTypes.indexOf(quad.selectionToSetIfClicked &&
          quad.selectionToSetIfClicked.rectType_);
      if (i !== -1) {
        found[i] = true;
      }
    }
    assert.deepEqual(found, [true, true, true]);
  });
});
</script>
